﻿Public Class ClsMMCollectionList
    'represents non-key based collection with special behaviour. Can include more than one object with the same value!

#Region "Attributes"
#Region "Private attributes"
    Private MoCollection As Collection = New Collection
#End Region
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property NumberOfItems() As Integer
        Get
            Return MoCollection.Count
        End Get
    End Property
#End Region
#End Region
#Region "Functions"
    'adds item to collection
    Public Function AddItem(ByVal IoValue As Object) As GeActionResult
        Try
            MoCollection.Add(IoValue)
            Return GeActionResult.kkOk

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'removes item from collection
    Public Function RemoveItemByValue(ByVal IoValue As Object, Optional ByVal IbSilentMode As Boolean = False) As GeActionResult
        Try
            Dim LiIndex As Integer = GetIndexOfItem(IoValue)
            If LiIndex <> -1 Then
                MoCollection.Remove(LiIndex)
                Return GeActionResult.kkOk
            End If
            Return GeActionResult.kkCancel
        Catch ex As Exception
            If IbSilentMode Then
                Return GeActionResult.kkCancel
            Else
                LogEventFromException(ex, , , )
                Return GeActionResult.kkError
            End If
        End Try
    End Function
    'gets item from collection defined by value
    Public Function GetIndexOfItem(ByVal IoValue As Object, Optional ByVal IbSilentMode As Boolean = False) As Integer
        Try
            For LiI As Integer = 1 To MoCollection.Count
                If MoCollection(LiI) = IoValue Then Return LiI
            Next

            Return -1
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return -1
        End Try
    End Function
    'gets if item defined by value is in collection
    Public Function IsItemInCollection(ByVal IoValue As Object) As Boolean
        Try
            Return GetIndexOfItem(IoValue) <> -1
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return False
        End Try
    End Function
    'gets collection of items
    Public Function GetCollection() As Collection
        Return MoCollection
    End Function
    'gets number of items in collection
    Public Function Count() As Integer
        Return MoCollection.Count
    End Function
    'determines if collection is empty
    Public Function IsEmpty() As Boolean
        Return MoCollection.Count = 0
    End Function

#End Region

End Class
